package zhihui.mobile.ui.register;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.MyEditText;
import com.zgm.utils.ui.UiHeaderLayout;

import java.util.Timer;
import java.util.TimerTask;

import zhihui.mobile.R;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.listener.EdDrawablePasswordListener;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.register.IRetrievePasswordContract;
import zhihui.mobile.presenter.register.RetrievePasswordPresenter;
import zhihui.mobile.ui.BaseActivity;
import zhihui.mobile.ui.login.LoginActivity;

/**
 * Created by 张公铭 on 2017/7/12 0012.
 */

public class RetrievePasswordActivity extends BaseActivity implements IRetrievePasswordContract.IRetrievePasswordView
{
    private UiHeaderLayout mHeader;
    private EditText ed_phone;
    private EditText ed_code;
    private MyEditText ed_password;
    private Button btn_msg;
    private Button btn_send;
    private Handler mHandler;
    private TextView tv_msg;

    private IRetrievePasswordContract.IRetrievePasswordPresenter retrievePasswordPresenter = new RetrievePasswordPresenter();
    private final int UPDATE_TIME = 1;
    private final int STOP_TIMER = 2;
    private Timer mTime = new Timer();

    @Override
    public void initView()
    {
        mHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                switch (msg.what)
                {
                    case UPDATE_TIME:
                        btn_msg.setText(msg.arg1 + "S");
                        break;
                    case STOP_TIMER:
                        stopTimer();
                        btn_msg.setText("获取验证码");
                        btn_msg.setEnabled(true);
                        break;
                }
            }
        };

        mHeader = (UiHeaderLayout) findViewById(R.id.header_retrieve);
        mHeader.setTitle("找回密码");
        mHeader.hindRightOneButton();
        mHeader.hindRightTwoButton();
        mHeader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(RetrievePasswordActivity.class);
            }
        });

        ed_phone = (EditText) findViewById(R.id.ed_retrieve_phone);
        UiUtils.editTextFocus(ed_phone);
        ed_code = (EditText) findViewById(R.id.ed_retrieve_phone_code);
        UiUtils.editTextFocus(ed_code);

        ed_password = (MyEditText) findViewById(R.id.ed_retrieve_password);
        ed_password.setDrawableRightListener(new EdDrawablePasswordListener(this));
        btn_msg = (Button) findViewById(R.id.btn_retrieve_get_code);
        btn_msg.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                retrievePasswordPresenter.getPhoneCode(ed_phone.getText().toString());
            }
        });

        btn_send = (Button) findViewById(R.id.btn_retrieve_password);
        btn_send.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                tv_msg.setText("");
                retrievePasswordPresenter.submit(ed_phone.getText().toString(), ed_code.getText().toString(), ed_password.getText().toString());
            }
        });

        tv_msg = (TextView) findViewById(R.id.tv_retrieve_password_msg);
        tv_msg.setText("");
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.retrieve_password_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{retrievePasswordPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        retrievePasswordPresenter.init(this, this);
    }

    private void stopTimer()
    {
        if (mTime != null)
        {
            mTime.cancel();
            mTime = null;
        }
    }

    @Override
    public void startTimer()
    {
        btn_msg.setEnabled(false);
        if (mTime == null)
        {
            mTime = new Timer();
        }
        mTime.schedule(new TimerTask()
        {
            int lastTime = 60;
            @Override
            public void run()
            {
                Message message = new Message();
                lastTime--;
                if (lastTime == 0)
                {
                    message.what = STOP_TIMER;
                } else
                {
                    message.what = UPDATE_TIME;
                    message.arg1 = lastTime;
                }
                mHandler.sendMessage(message);
            }
        }, 0, 1000);
    }

    @Override
    public void edPhoneFocuse()
    {
        UiUtils.editTextFocus(ed_phone);
    }

    @Override
    public void edCodeFocuse()
    {
        UiUtils.editTextFocus(ed_code);
    }

    @Override
    public void edPasswordFocuse()
    {
        UiUtils.editTextFocus(ed_password);
    }

    @Override
    public void showSuccessDialog(String message)
    {
        UiUtils.showDialog(this, null, message, "立即登录", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                goLoginActivity();
                dialog.dismiss();
                dialog.cancel();
                dialog = null;
            }
        });
    }

    private void goLoginActivity()
    {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        MyApplication.finishActivity(RetrievePasswordActivity.class);
    }

    @Override
    public void showMessage(String message)
    {
        tv_msg.setText(message);
    }
}
